package xdclass.cloud.redis.xdclass.cloud;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Component
public class RedisUtil {


    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    public boolean set(String key,Object value){
        try{
            redisTemplate.opsForValue().set(key,value);
            return true;
        }catch (Exception e){
            return false;
        }
    }

    public boolean set(String key,Object value,long time){
        try{
            redisTemplate.opsForValue().set(key,value,time, TimeUnit.SECONDS);
            return true;
        }catch (Exception e){
            return false;
        }
    }

    public Object get(String key){
        return key == null ? null : redisTemplate.opsForValue().get(key);
    }

    public boolean leftPushAll(String key,List<Object> list) {
        try{
            redisTemplate.opsForList().leftPushAll(key,list);
            return true;
        }catch (Exception e){
            return false;
        }
    }

    public List<Object> range(String key) {
        return this.range(key,0,-1);
    }

    public List<Object> range(String key,long start,long end) {
        return key == null ? null : redisTemplate.opsForList().range(key,start,end);
    }

    public void incr(String key) {
        redisTemplate.opsForValue().increment(key);
    }

}
